﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class frmNhapNgayNghiHoc : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        DataSet dsHK = new DataSet();

        public frmNhapNgayNghiHoc()
        {
            InitializeComponent();
        }


        //Lay gia tri ve tu user control thong qua event ValueChange
        private void userControl_ChonLOP1_ValueChanged(object sender,
                           CNCSoft.ErpApps.TMS.UserControl_ChonLOP.ValueChangedEventArgs e)
        {
            try
            {
                if (dsHK.Tables["DS_LOP"] != null)
                    dsHK.Tables["DS_LOP"].Clear();
                if (e == null)
                    dataAdapter = new SqlDataAdapter("SELECT STT_SoCai, MaHocTap, HoLot + ' ' +  Ten as HoTen " +
                                                     "FROM HOCSINH_LOP join HOCSINH on HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                     "WHERE MaLop = '" + userControl_ChonLOP1.MaLop + "' order by STT_SoCai", GlobalVariable.WSInfo.ConnectionString);
                else
                    dataAdapter = new SqlDataAdapter("SELECT STT_SoCai, MaHocTap, HoLot + ' ' +  Ten as HoTen " +
                                                     "FROM HOCSINH_LOP join HOCSINH on HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                     "WHERE MaLop = '" + e.NewValue + "' order by STT_SoCai", GlobalVariable.WSInfo.ConnectionString);
                dataAdapter.Fill(dsHK, "DS_LOP");
                lstDSHocSinh.DataSource = dsHK.Tables["DS_LOP"];
                lstDSHocSinh.ValueMember = "MaHocTap";
                lstDSHocSinh.DisplayMember = "HoTen";
            }
            catch { }
        }

        private void frmNhapNgayNghiHoc_Load(object sender, EventArgs e)
        {
            gridNgayNghiHoc.DataSource = bindingSource1;

            //Load user control chon LOP
            this.userControl_ChonLOP1 = new CNCSoft.ErpApps.TMS.UserControl_ChonLOP(GlobalVariable.WSInfo.getAppProperty("MaNienHocDefault"), GlobalVariable.WSInfo.ConnectionString);
            userControl_ChonLOP1.Top = 10;
            userControl_ChonLOP1.Left = 10;
            userControl_ChonLOP1.TabIndex = 0;
            this.Controls.Add(userControl_ChonLOP1);

            userControl_ChonLOP1.ValueChanged += userControl_ChonLOP1_ValueChanged;

            //Load du lieu trong event cua ListBox

            userControl_ChonLOP1_ValueChanged(userControl_ChonLOP1, null);
            
        }


        private void GetData(string strMaHocTap)
        {
            if (strMaHocTap == "" || strMaHocTap == null || strMaHocTap == "System.Data.DataRowView") return;
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                SqlCommand sqlCom = new SqlCommand("sp_Giaovu_LoadDiemDanh", cnn);
                sqlCom.CommandType = CommandType.StoredProcedure;

                sqlCom.Parameters.Add(new SqlParameter("@pMaHocTap", SqlDbType.VarChar)).Value = strMaHocTap;

                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom);

                if (dsHK.Tables["DIEMDANH"] != null)
                    dsHK.Tables["DIEMDANH"].Clear();

                dataAdapter.Fill(dsHK, "DIEMDANH");

                //table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                bindingSource1.DataSource = dsHK.Tables["DIEMDANH"];
                dsHK.Tables["DIEMDANH"].Columns["MaHocTap"].DefaultValue = strMaHocTap;
                dsHK.Tables["DIEMDANH"].Columns["CoPhep"].DefaultValue = false ;

                //SetEditMode(false);
                gridNgayNghiHoc.Focus();


            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }
        }

      

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

            string sqlInsert, sqlUpdate, sqlDelete;
            sqlInsert = "INSERT INTO HOCSINH_LOP_NGAYNGHI  (MaHocTap, NgayNghi, CoPhep, LyDo, MaHocKy) " +
                                     " VALUES (@pMaHocTap, @pNgayNghi, @pCoPhep, @pLyDo, @pMaHocKy) ";

            sqlUpdate = "UPDATE HOCSINH_LOP_NGAYNGHI SET MaHocTap=@pMaHocTap, NgayNghi=@pNgayNghi, CoPhep=@pCoPhep, LyDo=@pLyDo, MaHocKy=@pMaHocKy " +
                            " WHERE ID = @pID";

            sqlDelete = "DELETE FROM HOCSINH_LOP_NGAYNGHI WHERE ID=@pID";

            SqlParameter[] pInsert = new SqlParameter[5];
            SqlParameter[] pUpdate = new SqlParameter[6];
            SqlParameter[] pDelete = new SqlParameter[1];

            pInsert[0] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");
            pInsert[1] = new SqlParameter("@pNgayNghi", SqlDbType.DateTime, 16, "NgayNghi");
            pInsert[2] = new SqlParameter("@pCoPhep", SqlDbType.Bit, 1, "CoPhep");
            pInsert[3] = new SqlParameter("@pLyDo", SqlDbType.NVarChar, 100, "LyDo");
            pInsert[4] = new SqlParameter("@pMaHocKy", SqlDbType.VarChar, 2, "MaHocKy");

            pUpdate[0] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");
            pUpdate[1] = new SqlParameter("@pNgayNghi", SqlDbType.DateTime, 16, "NgayNghi");
            pUpdate[2] = new SqlParameter("@pCoPhep", SqlDbType.Bit, 1, "CoPhep");
            pUpdate[3] = new SqlParameter("@pLyDo", SqlDbType.NChar, 100, "LyDo");
            pUpdate[4] = new SqlParameter("@pMaHocKy", SqlDbType.VarChar, 2, "MaHocKy");
            pUpdate[5] = new SqlParameter("@pID", SqlDbType.Int, 16, "ID");
            
            pDelete[0] = new SqlParameter("@pID", SqlDbType.Int, 16, "ID");

            try
            {
                SqlCommand cmdInsert = new SqlCommand(sqlInsert, cnn);
                SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);
                SqlCommand cmdDelete = new SqlCommand(sqlDelete, cnn);

                cmdInsert.Parameters.AddRange(pInsert);
                cmdUpdate.Parameters.AddRange(pUpdate);
                cmdDelete.Parameters.AddRange(pDelete);

                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = cmdInsert;
                da.UpdateCommand = cmdUpdate;
                da.DeleteCommand = cmdDelete;

                da.Update(dsHK, "DIEMDANH");
                dsHK.AcceptChanges();

                //Load lai data
                GetData(lstDSHocSinh.SelectedValue.ToString());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            GetData(lstDSHocSinh.SelectedValue.ToString());
        }

        private void lstDSHocSinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDSHocSinh.SelectedIndex >-1)
                GetData(lstDSHocSinh.SelectedValue.ToString());
        }

        private void gridNgayNghiHoc_Click(object sender, EventArgs e)
        {

        }

    }
}
